# Contributor: Eric Molitor <eric@molitor.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=llvm-runtimes
# Note: Update together with llvm.
pkgver=19.1.2
_llvmver=${pkgver%%.*}
pkgrel=1
pkgdesc="LLVM Runtimes"
url="https://llvm.org/"
arch="all"
license="Apache-2.0"
makedepends="
	clang
	cmake
	linux-headers
	llvm$_llvmver-dev
	llvm$_llvmver-static
	python3
	samurai
	"
subpackages="
	libc++:libcxx
	libc++-static:libcxx_static
	libc++-dev:libcxx_dev
	compiler-rt:rt
	llvm-libunwind:libunwind
	llvm-libunwind-static:libunwind_static
	llvm-libunwind-dev:libunwind_dev
	"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/llvm-project-${pkgver//_/}.src.tar.xz
	armv6-arch.patch.noauto
	compiler-rt-lsan-dtp-offset.patch
	compiler-rt-ppc-fixes.patch
	compiler-rt-sanitizer-supported-arch.patch
	libunwind-link-libssp.patch
	fix-msan-with-musl.patch
	"
builddir="$srcdir/llvm-project-${pkgver//_/}.src"
options="!check"

case "$CARCH" in
# Sanitizers are broken on other arches.
# Keep in sync with compiler-rt-sanitizer-supported-arch.patch.
aarch64|ppc64le|x86_64|riscv64)
	_build_sanitizers='ON'
	;;
*)
	_build_sanitizers='OFF'
	;;
esac

prepare() {
	default_prepare

	case "$CARCH" in
	armhf)
		patch -Np1 < "$srcdir"/armv6-arch.patch.noauto
		;;
	esac
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		local crossopts="
			-DCMAKE_SYSTEM_NAME=Linux
			-DCMAKE_HOST_SYSTEM_NAME=Linux
			-DLIBUNWIND_SYSROOT=$CBUILDROOT
			"
	fi

	CC=clang \
	CXX=clang++ \
	CFLAGS="$CFLAGS -DNDEBUG" \
	CXXFLAGS="$CXXFLAGS -DNDEBUG" \
	cmake -B build -G Ninja -Wno-dev -S runtimes \
		-DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind;libcxx;libcxxabi" \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLIBCXX_HAS_MUSL_LIBC=ON \
		-DLIBUNWIND_HAS_NODEFAULTLIBS_FLAG=OFF \
		-DCOMPILER_RT_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DCOMPILER_RT_BUILD_SANITIZERS=$_build_sanitizers \
		-DCOMPILER_RT_INSTALL_PATH="/usr/lib/llvm$_llvmver/lib/clang/$_llvmver" \
		-DCOMPILER_RT_BUILD_GWP_ASAN=OFF \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		$crossopts
	cmake --build build
}

package() {
	# create target triple dir and a relative symlink to it
	local clangruntimedir="$(clang -print-runtime-dir)"
	install -dm0755 "$pkgdir"/"$clangruntimedir"
	ln -s "${clangruntimedir##*/}" "$pkgdir"/"${clangruntimedir%/*}"/linux

	DESTDIR="$pkgdir" cmake --install build

	install -dm0755 "$pkgdir"/usr/include/mach-o
	cp libunwind/include/*.h "$pkgdir"/usr/include/
	cp libunwind/include/mach-o/*.h "$pkgdir"/usr/include/mach-o/
}

libunwind() {
	pkgdesc="LLVM libunwind library"
	depends="!libunwind-dev"

	amove usr/lib/libunwind.so.*
}

libunwind_static() {
	pkgdesc="LLVM libunwind library (static)"

	amove usr/lib/libunwind.a
}

libunwind_dev() {
	pkgdesc="LLVM libunwind library (development files)"

	amove usr/lib/libunwind.so
	amove usr/include
}

rt() {
	pkgdesc="LLVM compiler-rt runtime libraries"

	amove usr/lib/llvm$_llvmver/lib/clang/$_llvmver
}

libcxx() {
	pkgdesc="LLVM libc++ library"

	amove usr/lib/libc++*.so.*
}

libcxx_static() {
	pkgdesc="LLVM libc++ library (static libs)"

	amove usr/lib/libc++*.a
}

libcxx_dev() {
	pkgdesc="LLVM libc++ library (development files)"

	amove usr/lib/libc++*.so
	amove usr/include/c++
}

sha512sums="
e98ee405f5c30004b39f0de0470400dd2965adf4cda4b47a1d3792ae362ed43eb45e96a9b1689f31e1064e0b39252379270bd95bf8081cf9b92ecb9b371bf43b  llvm-project-19.1.2.src.tar.xz
ed30960bc5dea6d611a691e12949ddff1346fb5ba0ff36741496bf36442468f3724bb98526d230a97e58848bef703d6b793db27e254d927004f1903e3f2816ed  armv6-arch.patch.noauto
4ffa63428e3558f490ed114db1524174d8142a8cd89d46baedf83995fcebd56b44030b8b4b3b3455e2f149102584daa597b72abad6c634835cad1117cefa853a  compiler-rt-lsan-dtp-offset.patch
23ded7dfa8b2a5c965cd0d09acb7483aa97fb8ddfb0a9637f7bf37484b8a66e5d44519f1649f45c3ca6e3c58cd3f326a30e5b365688a880be1bc17e78cb3a8a7  compiler-rt-ppc-fixes.patch
bd3f7b8929a08a3d3b412da6b248a7a2933069bafd8a8f3efea464886dd4a12e1b981c83d8330388221df62a5b49af1ed393068343b80a1de05f0176bb05b960  compiler-rt-sanitizer-supported-arch.patch
98eeabdeb08d6c9ed767cfd8a9ed29dc6732cd87235395b51245081299c7367194fb533e1c064249a32a98da68ae396f64e304a97a2d2b7dbdb874f77da160df  libunwind-link-libssp.patch
0a4f0b5ae82f93387e8880c6e293eef9234f0cb4dadf7c52846f1a45612b931d2b460579d08d48548de9a7e6372b75f95e05e32683a60911a3d48f567cd4808b  fix-msan-with-musl.patch
"
